package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ClaTimeAttendMapper;
import com.ruoyi.system.mapper.ScCourseClaMapper;
import com.ruoyi.system.mapper.ScStudentContactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ScStudentCourseMapper;
import com.ruoyi.system.service.IScStudentCourseService;

/**
 * 报名系统Service业务层处理
 * 
 * @author ZHANGQIBO
 * @date 2023-12-29
 */
@Service
public class ScStudentCourseServiceImpl implements IScStudentCourseService 
{
    @Autowired
    private ScStudentCourseMapper scStudentCourseMapper;

    @Autowired
    private ScCourseClaMapper scCourseClaMapper;
    @Autowired
    private ScStudentContactMapper scStudentContactMapper;
    @Autowired
    private ClaTimeAttendMapper claTimeAttendMapper;

    /**
     * 查询报名系统
     * 
     * @param studentCourseId 报名系统主键
     * @return 报名系统
     */
    @Override
    public ScStudentCourse selectScStudentCourseByStudentCourseId(Long studentCourseId)
    {
        return scStudentCourseMapper.selectScStudentCourseByStudentCourseId(studentCourseId);
    }

    @Override
    public List<ScStudentClaTime> selectScStudentCourseByclaId(Long claId) {
        List<ScStudentClaTime> scStudentClaTimes = scStudentCourseMapper.selectScStudentCourseByclaId(claId);
        for (ScStudentClaTime sc:
                scStudentClaTimes ) {
            sc.setContactList(scStudentContactMapper.selectScStudentContactBystudentId(sc.getStudentId()));

        }
        return scStudentClaTimes;
    }

    @Override
    public ClaTimeCollection getmobileClassList(ScClaTime scClaTime) {
        List<UpdateAttend> claTimeAttends = claTimeAttendMapper.selectClaTimeAttendByLeave(scClaTime.getCourseTimeId());


        List<ScStudentClaTime> scStudentClaTimes = scStudentCourseMapper.getmobileClassList(scClaTime.getClaId(),scClaTime.getCourseTimeId());
        ClaTimeCollection claTimeCollection = new ClaTimeCollection();
        claTimeCollection.setClaTimeAttends(claTimeAttends);
        claTimeCollection.setScStudentClaTimes(scStudentClaTimes);

        return claTimeCollection;
    }

    @Override
    public List<UpdateAttend> getmobileUpdateClassList(ScClaTime scClaTime) {
        List<UpdateAttend> claTimeAttends = claTimeAttendMapper.selectClaTimeAttendByTotal(scClaTime.getCourseTimeId());
        return claTimeAttends;
    }

    @Override
    public List<ScStudentClaTime> selectScStudentCourseByclaTimeAttend(ClaTimeAttend claTimeAttend) {
        List<ScStudentClaTime> scStudentClaTimes = scStudentCourseMapper.selectScStudentCourseByClaAttend(claTimeAttend);
        for (ScStudentClaTime sc:
                scStudentClaTimes ) {
            sc.setContactList(scStudentContactMapper.selectScStudentContactBystudentId(sc.getStudentId()));

        }
        return scStudentClaTimes;
    }

    /**
     * 查询报名系统列表
     * 
     * @param scStudentCourse 报名系统
     * @return 报名系统
     */
    @Override
    public List<ScStudentCourse> selectScStudentCourseList(ScStudentCourse scStudentCourse)
    {
        return scStudentCourseMapper.selectScStudentCourseList(scStudentCourse);
    }

    @Override
    public List<StudentApp> selectStudentAppList(Long claId) {
        return scStudentCourseMapper.selectStudentAppList(claId);
    }

    /**
     * 新增报名系统
     * 
     * @param scCourseClaAttend 报名系统
     * @return 结果
     */
    @Override
    public int insertScStudentCourse(List<ScCourseClaAttend> scCourseClaAttend)
    {
        Long studentId= scCourseClaAttend.get(0).getStudentId();
        String studentName= scCourseClaAttend.get(0).getStudentName();
        for (ScCourseClaAttend scCourseAttend:scCourseClaAttend
             ) {


        ScStudentCourse scStudentCourse = new ScStudentCourse();
                scStudentCourse.setCourseId(scCourseAttend.getCourseId());
                scStudentCourse.setStudentName(studentName);
                scStudentCourse.setStudentId(studentId);
                scStudentCourse.setCourseName(scCourseAttend.getCoursename());
                scStudentCourse.setBalanceHour(scCourseAttend.getTotaltime());
                scStudentCourse.setTotalHour(scCourseAttend.getTotaltime());
                scStudentCourse.setClaId(scCourseAttend.getClaId());
                scStudentCourse.setClaName(scCourseAttend.getClaName());
                scStudentCourse.setCreateUser(SecurityUtils.getUsername());
                scStudentCourse.setCreateTime(DateUtils.getNowDate());
            if ( scStudentCourseMapper.insertScStudentCourse(scStudentCourse)<=0){
                return  0;
            }
            scCourseClaMapper.updateScCourseClaPeople(scCourseAttend.getClaId());

        }
        return 1;
    }

    /**
     * 修改报名系统
     * 
     * @param scStudentCourse 报名系统
     * @return 结果
     */
    @Override
    public int updateScStudentCourse(ScStudentCourse scStudentCourse)
    {
        return scStudentCourseMapper.updateScStudentCourse(scStudentCourse);
    }

    /**
     * 批量删除报名系统
     * 
     * @param studentCourseIds 需要删除的报名系统主键
     * @return 结果
     */
    @Override
    public int deleteScStudentCourseByStudentCourseIds(Long[] studentCourseIds)
    {
        for (Long studentCourseId:
        studentCourseIds) {
            ScStudentCourse scStudentCourse = scStudentCourseMapper.selectScStudentCourseByStudentCourseId(studentCourseId);
            scCourseClaMapper.updateScCourseClaPeopleRemove(scStudentCourse.getClaId());

        }
        return scStudentCourseMapper.deleteScStudentCourseByStudentCourseIds(studentCourseIds);
    }

    /**
     * 删除报名系统信息
     * 
     * @param studentCourseId 报名系统主键
     * @return 结果
     */
    @Override
    public int deleteScStudentCourseByStudentCourseId(Long studentCourseId)
    {
        ScStudentCourse scStudentCourse = scStudentCourseMapper.selectScStudentCourseByStudentCourseId(studentCourseId);
        scCourseClaMapper.updateScCourseClaPeopleRemove(scStudentCourse.getClaId());
        return scStudentCourseMapper.deleteScStudentCourseByStudentCourseId(studentCourseId);
    }
}
